package com.train.schoolplan.user.controller;


import com.train.schoolplan.user.model.User;
import com.train.schoolplan.user.service.UserService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Description:文件描述：用户登录及管理
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    private static final Logger LOGGER = Logger.getLogger(UserController.class);


    @RequestMapping("tologin")
    public String toLogin() {

        LOGGER.debug("begin to login>>>>>>>>>");
        return "login";
    }


    @RequestMapping("dologin")
    public String doLogin(String username,
                          String pwd,
                          HttpSession session,
                          HttpServletRequest request) {
        LOGGER.info(username + "  begin login>>>>>");
        User loginUser = userService.login(username);

        if (loginUser == null) {
            LOGGER.error("用户名或者密码错误！");
            return "login";
        } else {
            if (loginUser.getPwd().equals(pwd)) {
                return "redirect:/bigscreen/index.html";
            } else {
                LOGGER.error("用户名或者密码错误！");
                return "login";
            }
        }
    }

    /**
     * 注册账号
     * @return
     */
    @RequestMapping("register.html")
    public String register() {

        LOGGER.debug("begin to register>>>>>>>>>");
        return "register";
    }


}
